package com.smartstate.iot.tool;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartstate.iot.config.SpringBeanUtils;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.mapper.TopicLogMapper;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.vo.TopicUpVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Slf4j
public class TopicTool {

    @Autowired
    private HandleService handleService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicLogMapper logMapper;
    /**
     * 上条时间
     */
    private Long er = 0L;

    /**
     * topic判断结果为盒子上行心跳数据  存储到数据库
     */
    public Boolean saveTopicConfig(TopicUpVo topicUpVo) {
        Boolean save = false;
        TopicConfig topicConfig = new TopicConfig();
        topicConfig.setTimesTamps(topicUpVo.getTimesTamp());
        String deviceType = topicUpVo.getContent().get(0).getDeviceType();
        topicConfig.setDeviceType(deviceType);
        topicConfig.setConfig(topicUpVo.getConfig());
        for (TopicUpVo.Content content : topicUpVo.getContent()) {
            topicConfig.setIp(content.getIp());
            topicConfig.setPort(content.getPort());
            topicConfig.setNewTopic(content.getNewTopic());
            topicConfig.setDeviceId(content.getDeviceId());
            topicConfig.setDeviceName(content.getDeviceName());
            topicConfig.setCpu(content.getCpu());
            topicConfig.setMomery(content.getMomery());
            TopicService topicService = SpringBeanUtils.getBean(TopicService.class);
            /**
             * 判断当前信息是新设备  还是已经存在设备
             *        1        新建设备更新设备信息
             *        2      如果已经存在就更新设备信息
             */
            if (handleService.logOnly(content).equals(true)) {
                /**
                 * 新增盒子心跳信息
                 */
                save = topicService.save(topicConfig);
                er = 0L;
            } else {
                /**
                 * 查询这个当前端口的上次心跳信息时间戳
                 */
                TopicConfig upTime = topicService.getUpTime(topicConfig);
                String timesTamps = upTime.getTimesTamps();
                er = Long.parseLong(timesTamps);
                /**
                 *  更新盒子心跳信息
                 */
                save = topicService.updateConfig(topicConfig, content);
            }
        }

        /**
         * 判断数据是 正常状态还是异常状态 或者  断线状态
         *   如果  1  当前数据时间戳与上条数据时间戳相隔小于10秒 并且content 设备信息无误  该设备状态为 正常
         *        2  当前数据时间戳与上条数据时间戳相隔小于10秒   单content 设备信息不完全   该设备状态为  异常
         *        3  当前数据时间戳与上条数据时间戳相隔大于10秒    该设备信息状态为   离线
         */
        //  如果盒子状态为离线,直接判断状态是否为异常
        // TODO 断线重连时状态为正常或异常,不能为离线
        String deviceId = topicUpVo.getContent().get(0).getDeviceId();
        String type = topicService.getTypeByDeviceId(deviceId);
        if ("离线".equals(type)){
            er = 0L;
        }
       /* if ("离线".equals(type)){
            TopicLog topicLog = new TopicLog();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new DateTime());
            topicLog.setDate(format);
            List<TopicUpVo.Content> contents = topicUpVo.getContent();
            for (TopicUpVo.Content content : contents) {
                if (handleService.getNull(content).equals(true)) {
                    *
                     * 设备信息对比正常  设备状态为正常

                    topicLog.setType("正常");
                    topicLog.setDetail("设备状态正常");
                    topicLog.setLogName(content.getDeviceName());
                    topicLog.setDeviceId(content.getDeviceId());
                    logMapper.insert(topicLog);
                    log.info(topicLog.getLogName()+"设备正常");
                } else {
                    // content对比信息不正确 设备定义为异常
                    topicLog.setType("异常");
                    topicLog.setDetail("设备状态异常");
                    topicLog.setLogName(content.getDeviceName());
                    topicLog.setDeviceId(content.getDeviceId());
                    logMapper.insert(topicLog);
                    log.info(topicLog.getLogName()+"设备异常!");
                }
            }
            return save;
        }*/
        //                对比时间戳是否在10秒内
        TopicLog topicLog = new TopicLog();
        List<TopicUpVo.Content> contentVo = topicUpVo.getContent();
        for (TopicUpVo.Content content : contentVo) {
                if (handleService.getNull(content).equals(true)) {
                    /**
                     * 设备信息对比正常  设备状态为正常
                     */
                    topicLog.setType("正常");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = simpleDateFormat.format(new DateTime());
                    topicLog.setDate(format);
                    topicLog.setDetail("设备状态正常");
                    topicLog.setLogName(content.getDeviceName());
                    topicLog.setDeviceId(content.getDeviceId());
                } else {
                    // content对比信息不正确 设备定义为异常
                    topicLog.setType("异常");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = simpleDateFormat.format(new DateTime());
                    topicLog.setDate(format);
                    topicLog.setDetail("设备状态异常");
                    topicLog.setLogName(content.getDeviceName());
                    topicLog.setDeviceId(content.getDeviceId());
                }
            /**
             * 判断当前心跳日志是新盒子的心跳还是 有盒子心跳状态改变了 需要存入
             */
            if (handleService.getLog(content).equals(true)) {
                handleService.save(topicLog);
            } else if (handleService.getUpLogType(topicLog).equals(true)) {
                handleService.save(topicLog);
            }
        }
        return save;
    }

    /**
     * 判断是否为首次
     *
     * @param topicUpVo
     * @return
     */
    public Boolean isFirstHeartbeat(TopicUpVo topicUpVo) {
        Boolean flag = false;
        List<TopicUpVo.Content> content = topicUpVo.getContent();
        for (TopicUpVo.Content content1 : content) {
            String deviceId = content1.getDeviceId();
            LambdaQueryWrapper<TopicConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TopicConfig::getDeviceId, deviceId);
            List<TopicConfig> list = topicService.list(queryWrapper);
            if (list.size() == 0) {
                flag = true;
            }
        }
        return flag;
    }
}

